package com.china08.yxyapi.api.manage.edu.office.innernotice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 教育局行政办公 内部通知 搜索通知
 * 
 */
@CrossOrigin
@RestController("EduInnerNoticeSendSrhController")
public class InnerNoticeSendSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/office/innerNotice/send/srh", method = RequestMethod.POST)
    public Page<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");

        YxyProfile yxyProfile = getProfile(request, response);
        // 当前教育局id
        String schoolId = yxyProfile.getSchoolId();
        // 当前用户的获取
        String userId = yxyProfile.getId();
        // 未删除状态
        int deleteStatus = 0;
        // 发件人列表
        Page<YxyNoticeInner> page = yxyNoticeInnerRepository.findBySchoolIdAndUserIdAndDeleteStatus(schoolId, userId, deleteStatus, pageable);
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        RespModel respModel = null;
        String receiverUserNick = null;
        for (YxyNoticeInner yxyNoticeInner : page.getContent()) {
            // JAVA 8 查询所有接受用户信息.多个用户以逗号分割，出现异常，返回空字符串
            // receiverUserNick = yxyNoticeInner.getAllReceiverUserNick(",");
            respModel = new RespModel();
            respModel.id = (yxyNoticeInner.getId());
            respModel.title = (yxyNoticeInner.getTitle());
            respModel.createdDate = (DateToolUtils.format(yxyNoticeInner.getCreatedDate(), "yyyy-MM-dd"));
            respModel.receiverUserNick = (receiverUserNick);

            // 获取未读人数
            List<YxyNotifictionTea> receiveList = yxyNoticeInner.getReceiver();
            Integer noReadCount = 0;

            List<Map<String, Object>> receiveUserList = new ArrayList<>();
            for (YxyNotifictionTea tea : receiveList) {
                Map<String, Object> map = new HashMap<String, Object>();
                Integer isRead = tea.getIsRead();
                String userNick = tea.getUserNick();
                String uId = tea.getUserId();
                if (isRead == 0) {
                    noReadCount = noReadCount + 1;
                }
                map.put("userNick", userNick);
                map.put("isRead", isRead);
                map.put("userId", uId);
                receiveUserList.add(map);
            }
            respModel.receiveUserList = receiveUserList;
            respModel.noReadCount = noReadCount;

            respModelLst.add(respModel);
        }
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, page.getTotalElements());
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel {
        // 消息id
        public String id;
        // 消息标题
        public String title;
        // 发布时间
        public String createdDate;
        // 接收用户名称
        public String receiverUserNick;

        // 未读人数
        public Integer noReadCount = 0;

        public List<Map<String, Object>> receiveUserList;
    }
}
